package com.bluexmicro.ota2.ui.screen.facecustom

import android.app.Activity.RESULT_OK
import android.content.Context
import android.content.Intent
import android.graphics.Bitmap
import android.net.Uri
import androidx.activity.result.contract.ActivityResultContract
import com.yalantis.ucrop.UCrop
import java.io.File


data class CropParam(
    val src: Uri,
    val dstWidth: Int,
    val dstHeight: Int
)

class ImageCropper : ActivityResultContract<CropParam, Uri?>() {

    override fun createIntent(context: Context, input: CropParam): Intent {
        val dst =
            Uri.fromFile(File(context.cacheDir.path + File.separator + System.currentTimeMillis() + ".jpeg"))

        return UCrop
            .of(input.src, dst)
            .withAspectRatio(
                input.dstWidth.toFloat(),
                input.dstHeight.toFloat()
            )
//            .withMaxResultSize(input.dstWidth, input.dstHeight)
            .getIntent(context)
    }


    override fun parseResult(resultCode: Int, intent: Intent?): Uri? {
        val inp = intent ?: return null
        return when (resultCode) {
            RESULT_OK /*&& requestCode == UCrop.REQUEST_CROP*/ -> {
                UCrop.getOutput(inp)
            }

            else -> {
                null
            }
        }
    }

}